package com.ftqh.project.travel.notes.entity;

import com.ftqh.project.travel.data.AbstractDomain;
import com.ftqh.project.travel.security.entity.User;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2017/3/29
 * \* Time: 15:10
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@Entity
@Table(name="travel_note")
@EntityListeners(AuditingEntityListener.class)
public class Note extends AbstractDomain<Long> {
    private static final long serialVersionUID = 4007385120995434198L;

    private String title;
    private String content;
    private String lng; //经度
    private String lat; //纬度
    private String remark;

    private String authorName;
    @ManyToOne
    @JoinColumn(name = "author_id")
    private User author;
    @LastModifiedDate
    private Long lastModified;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name="travel_note_id")
    private List<NoteImage> noteImages = new ArrayList<>();

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public LocalDateTime getLastModified() {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(this.lastModified), ZoneId.of("Asia/Shanghai"));
    }

    public void setLastModified(Long lastModified) {
        this.lastModified = lastModified;
    }

    public List<NoteImage> getNoteImages() {
        return noteImages;
    }

    public void setNoteImages(List<NoteImage> noteImages) {
        this.noteImages = noteImages;
    }

    @Override
    public String toString() {
        return "Note{" +
                "id='" + super.getId() + '\'' +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", lng='" + lng + '\'' +
                ", lat='" + lat + '\'' +
                ", remark='" + remark + '\'' +
                ", authorName='" + authorName + '\'' +
                ", author=" + author +
                ", createdAt=" + getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +
                ", lastModified=" + lastModified +
                '}';
    }
}
